<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口：网页可以根据设置的宽度自动进行适配，在浏览器的内部虚拟一个容器，容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比，为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>用户信息管理系统</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>
    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>
    <script>
        /**
         * 点击删除选中，执行删除
         * */
        function deleteUser(id) {
            if(confirm("确定删除吗？")){
                location.href="${pageContext.request.contextPath}/deleteServlet?id="+id;
            }
        }
        window.onload =function () {
            /**
             * 删除选中的用户信息
             * */
            document.getElementById("delSelect").onclick=function () {
                if (confirm("您了确定删除选中信息吗？")){
                    //防止空指针异常（未进行选定操作）
                    var flag=false;
                    var elementsByName = document.getElementsByName("uid");
                    for (let i = 0; i < elementsByName.length; i++) {
                        if (elementsByName[i].checked){
                            //有一个选中，就可以提交信息
                            flag=true;
                            break;
                        }
                    }
                    if (flag){
                        //找到指定的表单，调用submit进行提交信息(onsubmit是一个事件)
                        document.getElementById("form").submit();
                    }

                }

            }
            /**
             * 设置复选框全选功能
             * */
            document.getElementById("fistCheck").onclick=function () {
                //获取name为uid的标签存在数组中
                var elementsByName = document.getElementsByName("uid");
                for (let i = 0; i < elementsByName.length; i++) {
                    //设置所有复选框的状态与第一个复选框的状态相同
                    elementsByName[i].checked=this.checked;
                }

            }
        }
    </script>
</head>
<body>
<div class="container">
    <h3 style="text-align: center">用户信息列表</h3>
    <div style="float: left; margin-bottom: 3px">
        <form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet">
            <div class="form-group">
                <label for="exampleInputName2">姓名</label>
                <input type="text" class="form-control" name="name" id="exampleInputName2" value="${condition.name[0]}">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail2">籍贯</label>
                <input type="text" class="form-control" name="homeTown" id="exampleInputEmail2" value="${condition.homeTown[0]}">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail3">邮箱</label>
                <input type="email" class="form-control" name="email" id="exampleInputEmail3" value="${condition.email[0]}" >
            </div>
            <button type="submit" class="btn btn-default">查询</button>
        </form>
    </div>
    <div style="float: right;margin-bottom: 3px">
        <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.html">添加联系人</a>
        <a class="btn btn-primary" href="javascript:void(0);" id="delSelect">删除选中</a>
    </div>
    <form action="${pageContext.request.contextPath}/delSelectUserServlet" method="post" id="form">
        <table border="1" class="table table-bordered table-hover">
        <tr class="success">
            <th><input type="checkbox" id="fistCheck"></th>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>籍贯</th>
            <th>QQ</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${pageBean.list}" var="user" varStatus="s">
        <tr>
            <td><input type="checkbox" name="uid" value="${user.id}"></td>
            <td>${s.count}</td>
            <td>${user.name}</td>
            <td>${user.gender}</td>
            <td>${user.age}</td>
            <td>${user.homeTown}</td>
            <td>${user.qq}</td>
            <td>${user.email}</td>
            <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a>&nbsp;
                <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id})">删除</a></td>
        </tr>
        </c:forEach>
    </table>
    </form>
    <div>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <%--上一页--%>
                <c:if test="${pageBean.currentPage==1}">
                    <li class="disabled">
                </c:if>
                <c:if test="${pageBean.currentPage!=1}">
                    <li>
                </c:if>

                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pageBean.currentPage-1}&rows=5&name=${condition.name[0]}&homeTown=${condition.homeTown[0]}&email=${condition.homeTown[0]}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                    <%--分页动态显示显示--%>
                <c:forEach begin="1" end="${pageBean.totalPage}" varStatus="s" >
                    <c:if test="${pageBean.currentPage==s.count}">
                        <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${s.count}&rows=5&name=${condition.name[0]}&homeTown=${condition.homeTown[0]}&email=${condition.homeTown[0]}">${s.count}</a></li>
                    </c:if>
                    <c:if test="${pageBean.currentPage!=s.count}">
                        <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${s.count}&rows=5&name=${condition.name[0]}&homeTown=${condition.homeTown[0]}&email=${condition.homeTown[0]}">${s.count}</a></li>
                    </c:if>

                </c:forEach>
                    <%--下一条记录--%>
                <c:if test="${pageBean.currentPage==pageBean.totalPage}">
                    <li class="disabled">
                </c:if>
                <c:if test="${pageBean.currentPage!=pageBean.totalPage}">
                    <li>
                </c:if>
                    <c:if test="${pageBean.currentPage<pageBean.totalPage}">
                        <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pageBean.currentPage+1}&rows=5&name=${condition.name[0]}&homeTown=${condition.homeTown[0]}&email=${condition.homeTown[0]}" aria-label="Next">
                    </c:if>
                    <c:if test="${pageBean.currentPage>=pageBean.totalPage}">
                        <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pageBean.currentPage}&rows=5&name=${condition.name[0]}&homeTown=${condition.homeTown[0]}&email=${condition.homeTown[0]}" aria-label="Next">
                    </c:if>

                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <span style="font-size: 20px">共${pageBean.totalCount}条记录，共${pageBean.totalPage}页</span>
            </ul>

        </nav>
    </div>
</div>
</body>
</html>
